/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmTipeUserView.java
 *
 * Created on 22 Sep 12, 12:27:25
 */
package sistemperpustakaan.view.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import sistemperpustakaan.controller.UserController;
import sistemperpustakaan.model.UserModel;
import sistemperpustakaan.tablemodel.UserTypeTableModel;
import sistemperpustakaan.view.FrmMain;

/**
 *
 * @author EKo
 */
public class FrmTipeUserView extends javax.swing.JInternalFrame implements InternalFrameListener, ListSelectionListener {

    FrmMain frameMain;
    UserModel model;
    UserController controller;
    UserTypeTableModel tableModel;
    int idType = -1;
    private DialogDetailUserType userTypeDetail;

    /** Creates new form FrmTipeUserView */
    public FrmTipeUserView(FrmMain main) {
        initComponents();
        this.frameMain = main;
        model = new UserModel();
        controller = new UserController(this, model);
        addInternalFrameListener(this);
        tableType.getSelectionModel().addListSelectionListener(this);
        tableModel = new UserTypeTableModel();
        tableType.setModel(tableModel);
        tableType.getColumnModel().getColumn(0).setPreferredWidth(100);
        tableType.getColumnModel().getColumn(1).setPreferredWidth(300);
        tableType.getColumnModel().getColumn(2).setPreferredWidth(300);

        controller.findListUserType();

    }

    public UserController getController() {
        return controller;
    }

    public void setController(UserController controller) {
        this.controller = controller;
    }

    public FrmMain getFrameMain() {
        return frameMain;
    }

    public void setFrameMain(FrmMain frameMain) {
        this.frameMain = frameMain;
    }

    public int getIdType() {
        return idType;
    }

    public void setIdType(int idType) {
        this.idType = idType;
    }

    public UserModel getModel() {
        return model;
    }

    public void setModel(UserModel model) {
        this.model = model;
    }

    public UserTypeTableModel getTableModel() {
        return tableModel;
    }

    public void setTableModel(UserTypeTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public JTable getTableType() {
        return tableType;
    }

    public void setTableType(JTable tableType) {
        this.tableType = tableType;
    }

    public JTextField getTxtId() {
        return txtId;
    }

    public void setTxtId(JTextField txtId) {
        this.txtId = txtId;
    }

    public JTextField getTxtNama() {
        return txtNama;
    }

    public void setTxtNama(JTextField txtNama) {
        this.txtNama = txtNama;
    }

    public DialogDetailUserType getUserTypeDetail() {
        return userTypeDetail;
    }

    public void setUserTypeDetail(DialogDetailUserType userTypeDetail) {
        this.userTypeDetail = userTypeDetail;
    }

    public JLabel getLblJumlah() {
        return lblJumlah;
    }

    public void setLblJumlah(JLabel lblJumlah) {
        this.lblJumlah = lblJumlah;
    }

    public void setAction() {
        frameMain.getBtnCari().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.findListUserType();
                }
            }
        });

        frameMain.getBtnRefresh().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.refreshListUserType();
                }
            }
        });

        frameMain.getBtnDetail().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (idType > -1 && isSelected()) {
                    userTypeDetail = new DialogDetailUserType(frameMain, true, controller, idType);
                    userTypeDetail.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");
                }
            }
        });

        frameMain.getBtnTambah().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    userTypeDetail = new DialogDetailUserType(frameMain, true, controller);
                    userTypeDetail.setVisible(true);
                }
            }
        });

        frameMain.getBtnHapus().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (idType > -1 && isSelected()) {
                    controller.saveDeleteUserType();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");
                }
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtNama = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableType = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        lblJumlah = new javax.swing.JLabel();
        gbr = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Tipe User");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Tipe ID");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 14, -1, -1));

        jLabel2.setText("Nama Tipe");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));
        getContentPane().add(txtId, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 10, 41, -1));
        getContentPane().add(txtNama, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 111, -1));

        tableType.setAutoCreateRowSorter(true);
        tableType.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableType.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tableType);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 410, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 90, 410, 260));

        jLabel3.setText("item(s) found");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 70, -1, 20));

        lblJumlah.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblJumlah.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblJumlah.setText("0");
        getContentPane().add(lblJumlah, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 70, 46, -1));

        gbr.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        gbr.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/typeUser.png"))); // NOI18N
        getContentPane().add(gbr, new org.netbeans.lib.awtextra.AbsoluteConstraints(245, 0, 110, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setText("Data Tipe User");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 10, 107, 27));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel gbr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblJumlah;
    private javax.swing.JTable tableType;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtNama;
    // End of variables declaration//GEN-END:variables

    public void internalFrameOpened(InternalFrameEvent e) {
    }

    public void internalFrameClosing(InternalFrameEvent e) {
    }

    public void internalFrameClosed(InternalFrameEvent e) {
        //frameMain.getDesktopPane().selectFrame(true);
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    }

    public void internalFrameActivated(InternalFrameEvent e) {
        setAction();
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
    }

    public void valueChanged(ListSelectionEvent e) {
        try {
            idType = Integer.valueOf(tableType.getValueAt(tableType.getSelectedRow(), 0).toString());
        } catch (Exception ex) {
        }
    }
}
